# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: read_util.py
# Time: 19:17


import configparser
import json
from configparser import ConfigParser

import yaml

from common.utils.file_util import get_data_yaml, get_data_json,get_real_ini_dir


class MyConfigParser(ConfigParser):
    # 重写 configparser 中的 optionxform 函数，解决 .ini 文件中的 键option 自动转为小写的问题
    def __init__(self, defaults=None):
        ConfigParser.__init__(self, defaults=defaults)

    def optionxform(self, optionstr):
        return optionstr


class ReadFileData():

    def load_yaml(self, file_path):
        file = get_data_yaml(file_path)
        with open(file, encoding='utf-8') as f:
            data = yaml.safe_load(f)
        return data

    def load_json(self, file_path):
        file = get_data_json(file_path)
        with open(file, encoding='utf-8') as f:
            data = json.load(f)
        return data

    def load_ini(self, file_path):
        file = get_real_ini_dir(file_path)
        config = MyConfigParser()
        config.read(file, encoding="UTF-8")
        data = dict(config._sections)
        return data


class OperationIni():
    def __init__(self, filename):
        self.file_path = get_real_ini_dir(filename)

    def get_ini_file_data(self):
        '''
        读取ini配置文件所有节点信息
        :param file_path: ini配置文件下的绝对路径
        :return: 返回ini文件中所有节点信息，当文件不存在或没有数据时返回空字典：{}
        '''
        config = configparser.ConfigParser()
        config.read(self.file_path, encoding="utf-8")
        dict_data = dict()
        for item in config.items():
            temp_dict = dict()
            if item[0] == "DEFAULT":
                continue
            line = config.items(item[0])
            temp_dict[item[0]] = dict(line)
            dict_data.update(temp_dict)
        return dict_data

    def get_ini_file_path_section_data(self, section_name):
        '''
        读取ini配置文件指定节点信息
        :param section_name:节点名称
        :return:返回ini文件中指定节点信息，当文件不存在或没有数据时返回空字典：{}
        '''
        config = configparser.ConfigParser()
        config.read(self.file_path, encoding="utf-8")
        dict_data = {}
        if config.has_section(section_name):
            options = config.options(section_name)
            for option in options:
                kv_dict = {}
                item = config.get(section_name, option)
                kv_dict[option] = item
                dict_data.update(kv_dict)
            return dict_data
        else:
            return {}

    def get_ini_option_data(self, section_name, option_name):
        '''
        获取配置文件下制定节点下制定键的值
        :param file_path:ini配置文件绝对路径
        :param section_name:节点名称
        :param option_name:节点下键值
        :return:返回指定节点下指定键的值，当文件不存在或节点或键不存在时返回None
        '''
        config = configparser.ConfigParser()
        config.read(self.file_path, encoding="utf-8")
        if config.has_section(section_name):
            if config.has_option(section_name, option_name):
                value = config.get(section_name, option_name)
                return value
        else:
            return None


if __name__ == '__main__':
    # print(set_state("api_test_data.yml"))
    from common.utils.file_util import get_data_json
    # print(OperationIni(get_real_ini_dir("setting.ini")).get_ini_file_path_section_data("thread").get("set"))
    # print(ReadFileData().load_yaml("api_test_data.yml")["test_gold_deposits"])
    # file = get_data_yaml("all_requests_information.yml")
    print(ReadFileData().load_yaml("api_test_data.yml").get("test_access_to_all_students"))